import {getDepositsList, getDepositById, release, getDepositReleaseList} from '../../services/capital/deposit';
import {parse} from 'qs';
import { message } from 'antd';
import Constants from '../../utils/Constants';
const dateFormat = 'YYYY-MM-DD';

export default {
    namespace: 'deposit',
    state: {
        search: { // 查询条件
            currencyCode: '',
            status: '0',
            depositNo: "",
            cstId: "",
            beginTime: "",
            endTime: ""

        },
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: [],
        editModel: { // 编辑视图
            defaultWidth: 800, // 缺省宽度800
            width: 1000, // 宽度
            showStatus: false, // 默认不显示
            confirmLoading: false, // 确认按钮loading状态
            title: '释放保证金', // 标题
            step: 1,
            form:{},
            releasePage: {
                page: 1,
                size: 10,
                totalRow: 0,
                totalPage: 0
            },
            releaselist: [],

        }
    },
    reducers: {

        /**
         * 设置查询条件
         *
         * @param state
         * @param actio
         * @returns {{search}}
         */
        setSearch(state, action) {
            return {...state, search: action.payload.search};
        },

        /**
         * 是否显示
         *
         * @param state
         * @param action
         * @returns {{showStatus: (boolean|*)}}
         */
        setEditModel(state, action) {
            let payload = action.payload;

            let newState = {...state.editModel};
            if(payload.showStatus != undefined) {
                newState.showStatus = payload.showStatus;
            }
            if(payload.width != undefined) {
                newState.width = payload.width;
            }
            if(payload.title != undefined) {
                newState.title = payload.title;
            }
            if(payload.confirmLoading != undefined) {
                newState.confirmLoading = payload.confirmLoading;
            }
            if(payload.step != undefined) {
                newState.step = payload.step;
            }
            if(payload.type != undefined) {
                newState.type = payload.type;
            }
            if(payload.form != undefined) {
                newState.form = payload.form;
            }
            if(payload.releasePage != undefined) {
                newState.releasePage = payload.releasePage;
            }
            if(payload.releaselist != undefined) {
                newState.releaselist = payload.releaselist;
            }
            if(payload.forex_relate_id!=undefined)
            {
                newState.forex_relate_id=payload.forex_relate_id;
            }

            return {...state, editModel: {...newState}};
        },

        /**
         * 设置数据列表
         *
         * @param state
         * @param action
         */
        setList(state, { payload }) {
            return {...state, list: payload.list, page: payload.page}
        },

        /**
         * 设置页码
         *
         * @param state
         * @param payload
         * @returns {{page: *}}
         */
        setPage(state, { payload }) {
            return {...state, page: payload.page}
        },

        /**
         * 设置页码
         *
         * @param state
         * @param payload
         * @returns {{page: *}}
         */
        setReleasePage(state, { payload }) {
            let newState = {...state.editModel};
            newState.releasePage = payload.releasePage;
            return {...state, editModel: {...newState}};
        }

    },
    effects: {

        /**
         * 获取保证金列表
         * @param call
         * @param put
         * @param select
         */
        *getDepositsList({}, {call, put, select}) {
            const search = yield select(state => state.deposit.search);
            const page = yield select(state => state.deposit.page);

            if (search.time != null && search.time != undefined) {
                if (search.time.length >= 2) {
                    if (search.time[0] != '') {
                        search.beginTime = search.time[0].format(dateFormat);
                    }
                    if (search.time[1] != '') {
                        search.endTime = search.time[1].format(dateFormat);
                    }

                }
            }

            const res = yield call(getDepositsList, {
                status: search.status,
                depositNo: search.depositNo,
                cstId: search.cstId,
                beginTime: search.beginTime,
                endTime: search.endTime,
                page
            });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                yield put({
                    type: 'setList',
                    payload: {
                        list: content.list,
                        page: content.page == undefined ? Constants.DEFAULTPAGE : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取保证金
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getDepositById({ payload }, { call, put, select }) {
            const res = yield call(getDepositById, { id: payload.id});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'getDepositReleaseList',
                    payload: {
                        form: {
                            id:  content.id,
                            customerno:content.customerno,
                            depositno: content.depositno,
                            createtime: content.createtime,
                            cstid: content.cstid,
                            amount: content.amount,
                            unpayamount: content.unpayamount,
                            fxamount: content.fxamount,
                            recamount: content.recamount,
                            rate: content.rate,
                            status: content.status,
                            isstated: content.isstated,
                            stateid: content.stateid,
                            forexid: content.forexid,
                            type : payload.type


                        }
                    }
                });
            } else {
                if(payload.onFail)payload.onFail();
                message.error(res.data.errMessage);
            }
        },


        /**
         * 释放保证金
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *release({ payload }, { call, put, select }) {
            const res = yield call(release, payload);
            if (res.data.resultCode === "ok") {
                message.info("释放成功！");
                yield put({
                    type: 'getDepositById',
                    payload: {
                        id: payload.depositid,
                        type : payload.type
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 释放保证金列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getDepositReleaseList({payload}, {call, put, select}) {
            const page = yield select(state => state.deposit.editModel.releasePage);

            const res = yield call(getDepositReleaseList, {
                id: payload.form.id,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                // payload.form.list2 =  content.list,
                // payload.releasePage =  content.page,

                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: true,
                        confirmLoading: false,
                        title: "保证金详情",
                        type: 'update',
                        form: payload.form,
                        releasePage : content.page,
                        releaselist : content.list,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },



    },
    subscriptions: {
        setup({history, dispatch}) {
            return history.listen(({pathname}) => {
                dispatch({
                    type: 'getDepositsList'
                });
            });
        }
    }
};
